<template>
  <div>
    <!-- 顶部搜索 -->
    <div class="search-row search-box">
      <el-form :inline="true" :model="searchParams" class="demo-form-inline">

        <el-form-item label="人审时间">
          <el-date-picker @change="getTime" v-model="timeRange" type="datetimerange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="success" icon="el-icon-search" @click="handleSearch">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="success" plain @click="handleReset">重置</el-button>
        </el-form-item>

      </el-form>
    </div>

    <!--table 刷新-->
    <div class="table-conBtn">
      <freshCom @searchList="searchList"></freshCom>
    </div>

    <!--table 列表-->
    <div class="table-box">
      <el-table ref="table" :data="tableData" v-loading="loading" :fit="true" max-height="490" stripe>
        <el-table-column type="index" label="序号">
          <template slot-scope="scope">
            {{ scope.$index | filterPage(pagingParams.pageNum, pagingParams.pageSize) }}
          </template>
        </el-table-column>

        <el-table-column label="类型" prop="jobType" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.jobType | jobTypeFilter }}</span>
          </template>
        </el-table-column>

        <el-table-column label="配文" prop="jobContent" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.jobContent" v-html="filterContent(scope.row.jobContent)">
              {{ scope.row.jobContent }}
            </span>
            <span v-else> --- </span>
          </template>
        </el-table-column>

        <el-table-column label="视图" prop="jobImagesAddress" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.jobType === 'TEXT'">
              <listImgString :imgInfo="scope.row.jobImagesAddress"></listImgString>
            </span>

            <span v-else @click="videoPlay(scope.row.jobImagesAddress)" class="img-box">
              <div class="img-inner-box">
                <el-image :src="scope.row.imgInfo"></el-image>
              </div>
            </span>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" prop="jobSubmitTime" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.jobSubmitTime | timeToTime }}</span>
          </template>
        </el-table-column>

        <el-table-column label="未通过原因" prop="reason" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.reason }}</span>
          </template>
        </el-table-column>

        <el-table-column label="人审时间" prop="peopleApprovedTime" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.peopleApprovedTime">{{ scope.row.peopleApprovedTime }}</span>
            <span v-else> --- </span>
          </template>
        </el-table-column>

        <el-table-column label="审核人" prop="peopleApprovedUserName" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.peopleApprovedUserName">{{ scope.row.peopleApprovedUserName }}</span>
            <span v-else> --- </span>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 视频组件 -->
    <showVideo v-if="videoData.isShow" :video-data="videoData"></showVideo>

    <!--分页-全局组件-->
    <!-- <pageCom :searchParams="pagingParams" :total="total" @searchList="searchList"></pageCom> -->
    <jobPage :searchParams="pagingParams" :tableData="tableData" :hasTotal="true" :pageLoading="pageLoading" :total="total" @changePage="changePage"></jobPage>

  </div>
</template>

<script>

import jobPage from '@common/jobPage'
import showVideo from '@common/showVideo'
import listImgString from '@common/listImgString'
import { getVideoImg } from '@utils/getFaceImg'
import { contentToStr, getVideoPara } from '@utils/pubFunction'
import { ansyCountAPI } from '@/api/contentCheck/jobContent'
import { getHomeworkList } from '@/api/commentCheck/unpassList'

const jobType = {
  TEXT: { text: '图文'},
  VIDEO: { text: '短视频'}
}

export default {
  msg: '作业帖子',

  components: {
    jobPage,
    showVideo,
    listImgString
  },

  data() {
    return {
      total: 0,
      loading: true,
      pageLoading: false,

      tableData: [],
      tableTemp: [],
      tempSize: 0,

      timeRange: [],

      pagingParams: {
        pageNum: 1,
        pageSize: 10
      },

      searchParams: {
        timeType: 'peopleApprovedTime', // 时间类型
        startTime: '',
        endTime: '',
        checkStatu: 'UNPASS', // 人审状态
        order: 'peopleApprovedTime', // 人审时间
        isAsc: false, // 人审时间倒序
        userType: 'DAPENGACCOUNT', // 大鹏号
        userValue: '', // 大鹏号
      },

      videoData: {
        isShow: false,
        vid: ''
      },

      polivNum: {
        polyvUserId: '',
        polyvSecretKey: ''
      }
    }
  },

  computed: {
    dpAccount() {
      return this.$route.query.dpAccount
    }
  },

  filters: {
    jobTypeFilter (type) {
      return jobType[type].text
    }
  },

  created() {
    if(this.dpAccount) {
      this.searchParams.userValue = this.dpAccount
    }

    this.searchList()
  },

  methods: {
    getTime(val){
      if(!val){
        this.searchParams.startTime = ''
        this.searchParams.endTime = ''
      }
    },
    searchList(checkPage = true) {
      this.loading = true

      if(this.timeRange) {
        this.searchParams.startTime = this.timeRange[0]
        this.searchParams.endTime = this.timeRange[1]
      }
      
      let params = { ...this.searchParams, ...this.pagingParams }

      getHomeworkList(params, (res) => {
        this.loading = false
        this.tableTemp = res.rows
        this.tableData = res.rows
        this.tempSize = res.rows.length
        this.getPolivNum()
        
      },(err) => {
        this.$message({
          message: err.message,
          type: 'warning'
        })
      })

      if (!checkPage) return

      this.pageLoading = true

      //查询总条数
      ansyCountAPI(params, (res) => {
        this.pageLoading = false
        this.total = res
      },(err) => {
        this.$message({
          message: err.message,
          type: 'warning'
        })
      })
    },

    // 播放视频
    videoPlay(vid) {
      if (!vid) {
        this.$message.warning('无视频资源！')
        return
      }

      this.videoData.vid = vid
      this.videoData.isShow = true
    },

    // 获取系统配置-保利威视
    getPolivNum() {
      let _this = this

      getVideoPara().then( (res) => {

          this.polivNum.polyvUserId = res.polyvUserId
          this.polivNum.polyvSecretKey = res.polyvSecretKey

          let n = 0

          if (!this.tableTemp || this.tableTemp.length === 0) { return }

          this.tableTemp.forEach((item, index) => {
            if (item.jobType === 'VIDEO') {
              _this.getImg(item.jobImagesAddress, index).then( (data) => {
                n++
                _this.setData(n)
              }, () => {
                n++
                 _this.setData(n)
              })
            }
          })
        }, (res) => {
          _this.tableData = [..._this.tableTemp]
        }
      )
    },

    // 符合条件后, 给table赋值
    setData(n) {
      if (n === this.tempSize) {
        this.loading = false
        this.tableData = [...this.tableTemp]
      }
    },

    // 获取封面图
    getImg(video, index) {
      if (!video) return Promise.resolve(false)
      let _this = this
      return getVideoImg(video, this.polivNum).then(
        (res) => {
          _this.tableTemp[index].imgInfo = res
          return Promise.resolve(true)
        },
        (error) => {
          return Promise.reject(error)
        }
      )
    },

    // 过滤返回评论内容
    filterContent(item) {
      return contentToStr(item)
    },

    // 查询
    handleSearch() {
      this.pagingParams.pageNum = 1
      this.searchList()
    },

    // 重置
    handleReset() {
      this.timeRange = []
    },

    /* 换页 */
    changePage(page) {
      this.searchList(false)
    }
  }
}
</script>
